package com.wl156.activities.order;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParserException;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import com.wl156.R;
import com.wl156.activities.find.FindResultBase;
import com.wl156.adapters.OrderAdapter;

public class MyOrderResult extends FindResultBase {

	String endLocationLv1;
	String endLocationLv2;
	String endLocationLv3;
	String freightMessageId;
	String CargosDescription;
	String driverName;
	String plateNumber;
	String driverTel;
	String Merchandiser;
	String MerchandiserTel;
	String tonnage;
	String truckTonnageSymBol;
	String acceptDateStart;
	String acceptDateEnd;
	String[] orderStatus;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mHeaderStub.setLayoutResource(R.layout.item_order_header);
		mHeaderStub.inflate();

		Bundle extras = getIntent().getExtras();
		
		hideBottomButtons();

		startLocationLv1 = extras.getString("startLocationLv1");
		startLocationLv2 = extras.getString("startLocationLv2");
		startLocationLv3 = extras.getString("startLocationLv3");
		endLocationLv1 = extras.getString("endLocationLv1");
		endLocationLv2 = extras.getString("endLocationLv2");
		endLocationLv3 = extras.getString("endLocationLv3");
		freightMessageId = extras.getString("freightMessageId");
		CargosDescription = extras.getString("CargosDescription");
		driverName = extras.getString("driverName");
		plateNumber = extras.getString("plateNumber");
		driverTel = extras.getString("driverTel");
		Merchandiser = extras.getString("Merchandiser");
		MerchandiserTel = extras.getString("MerchandiserTel");
		tonnage = extras.getString("tonnage");
		truckTonnageSymBol = extras.getString("truckTonnageSymBol");
		acceptDateStart = extras.getString("acceptDateStart");
		acceptDateEnd = extras.getString("acceptDateEnd");
		orderStatus = new String[] { extras.getString("orderStatus") };

		new FindTask(type).execute();
	}

	@Override
	protected int getPhoneIndex() {
		return 11;
	}

	@Override
	protected String getFindName() {
		return "我的订单";
	}

	@Override
	protected Class getFindActivity() {
		return MyOrder.class;
	}

	@Override
	protected Class getFindResultActivity() {
		return MyOrderResult.class;
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);

		Intent i = new Intent(MyOrderResult.this, MyOrderDetail.class);
		i.putExtra("id", String.valueOf(id));
		startActivity(i);
	}

	@Override
	protected void onLastItemVisible() {
		super.onLastItemVisible();

		new FindTask(type).execute();
	}

	public class FindTask extends FindTaskBase {

		public FindTask(int type) {
			super(type);
		}

		@Override
		protected void onLoadFailed() {
			super.onLoadFailed();

			new FindTask(type).execute();
		}

		@Override
		protected Object doInBackground(Void... params) {
			try {
				return mWebServiceClient.companyGetMyOrders(mCertificate,
						startLocationLv1, startLocationLv2, startLocationLv3,
						endLocationLv1, endLocationLv2, endLocationLv3,
						freightMessageId, CargosDescription, driverName,
						plateNumber, driverTel, Merchandiser, MerchandiserTel,
						tonnage, truckTonnageSymBol, acceptDateStart,
						acceptDateEnd, mCurrentPageIndex, countPerPage,
						orderStatus);
			} catch (IOException e) {
				e.printStackTrace();
				return e;
			} catch (XmlPullParserException e) {
				e.printStackTrace();
				return e;
			}
		}
	}

	@Override
	protected Class getAdapter() {
		return OrderAdapter.class;
	}

}
